package com.demo.thread_interrupt;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author shenguangyang
 */
public class InterruptExample05 {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<?> future = executorService.submit(() -> {
            try {
                while (!Thread.interrupted()) {
                    System.out.println("Thread run");
                }
                 System.out.println("Thread end");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        TimeUnit.MILLISECONDS.sleep(10);
        future.cancel(true);
        System.out.println("Main run");
    }
}
